package com.suning.sawp.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.suning.sawp.constants.RegAndBindErrCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.register.LocateMemberRtn;
import com.suning.sawp.dto.register.RegisterInfoRtn;
import com.suning.sawp.intf.register.RegisterService;

/**
 * 用户快速注册, 绑定用户关系
 * 
 * @author 14062606
 * @see [相关类/方法]
 * @since 
 */
@Controller
@RequestMapping("/register")
public class RegisterAndBindController {
	 /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(RegisterAndBindController.class);
    
    /**
     * 定位云商会员接口
     */
    @Resource
    RegisterService registerService;
    
    @RequestMapping("/locateMemberByMobile.do")
    @ResponseBody
    public String locateMemberByMobile(String mobile){
    	LOGGER.debug("开始会员验证，mobile:{}",mobile);
    	ReturnMsg<LocateMemberRtn> ret = new ReturnMsg<LocateMemberRtn>();
    	GsonBuilder gsonbulid = new GsonBuilder().disableHtmlEscaping();
    	Gson gson = gsonbulid.create();
    	
    	if (StringUtils.isBlank(mobile)){
    		ret.setRetFlag(ReturnMsg.FAIL);
    		ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E888);
			ret.setErrorMessage(RegAndBindErrCodeConstants.COMMON_ERR_MAP
	    			.get(RegAndBindErrCodeConstants.COMMON_ERR_E888));
			return gson.toJson(ret);
    	}
    	ret = registerService.locateMemberByMobileNo(mobile);
    	return gson.toJson(ret);
    }
    
    @SuppressWarnings("rawtypes")
	@RequestMapping("/sendMessage.do")
    @ResponseBody
    public String sendMessage(String mobile, HttpServletRequest request){
    	LOGGER.debug("开始发送短信验证码给用户，mobile:{}", mobile);
    	ReturnMsg ret = new ReturnMsg();
    	GsonBuilder gsonbulid = new GsonBuilder().disableHtmlEscaping();
        Gson gson = gsonbulid.create();
        String requestIp = request.getRemoteAddr();
        
        if(StringUtils.isBlank(mobile)){
    		ret.setRetFlag(ReturnMsg.FAIL);
    		ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E888);
			ret.setErrorMessage(RegAndBindErrCodeConstants.COMMON_ERR_MAP
	    			.get(RegAndBindErrCodeConstants.COMMON_ERR_E888));
			return gson.toJson(ret);
        }
        ret = registerService.sendShortMessageToUser(mobile, requestIp);
    	return gson.toJson(ret);
    }
    
    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param mobile
     * @param staffCode
     * @param deviceType
     * @param validCode
     * @param source 
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/createOfflineAccount.do")
    @ResponseBody
    public String createOfflineAccount(String mobile, String staffCode, String deviceType
            , String validCode, String source){
    	Object[] argsArr = {mobile, staffCode, deviceType, validCode};
		LOGGER.debug("开始注册线下会员，mobile:{}, staffCode:{}, deviceType:{}, validCode:{}",argsArr);
		ReturnMsg<RegisterInfoRtn> ret = new ReturnMsg<RegisterInfoRtn>();
		GsonBuilder gsonbulid = new GsonBuilder().disableHtmlEscaping();
		Gson gson = gsonbulid.create();
		
		if (StringUtils.isBlank(mobile) || StringUtils.isBlank(staffCode) || 
				StringUtils.isBlank(deviceType) || StringUtils.isBlank(validCode)){
    		ret.setRetFlag(ReturnMsg.FAIL);
    		ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E888);
			ret.setErrorMessage(RegAndBindErrCodeConstants.COMMON_ERR_MAP
	    			.get(RegAndBindErrCodeConstants.COMMON_ERR_E888));
			return gson.toJson(ret);
		}
	    
	    try {
	    	ret = registerService.createOfflineAccount(staffCode, 
	    			deviceType, mobile, validCode, source);
	    } catch (Exception e){
	    	ret.setRetFlag(ReturnMsg.FAIL);
	    	ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E999);
	    	ret.setErrorMessage(RegAndBindErrCodeConstants.COMMON_ERR_MAP
	    			.get(RegAndBindErrCodeConstants.COMMON_ERR_E999));
	    	Object[] errArgs = {mobile, staffCode, deviceType, validCode, e};
	    	LOGGER.error("注册线下会员发生异常:mobile:{}, staffCode:{}, " +
	    			"deviceType:{}, validCode:{}, error:{}", errArgs);
	    }
    	return gson.toJson(ret);
    }
    
    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param mobile
     * @param staffCode
     * @param deviceType
     * @param validCode
     * @param source
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/createOnlineAndOfflineAccount.do")
    @ResponseBody
    public String createOnlineAndOfflineAccount(String mobile, String staffCode, String deviceType
            , String validCode, String source){
    	Object[] argsArr = {mobile, staffCode, deviceType, validCode};
		LOGGER.debug("开始注册线上线下会员，mobile:{}, staffCode:{}, deviceType:{}, validCode:{}",argsArr);
		ReturnMsg<RegisterInfoRtn> ret = new ReturnMsg<RegisterInfoRtn>();
    	GsonBuilder gsonbulid = new GsonBuilder().disableHtmlEscaping();
        Gson gson = gsonbulid.create();
        
        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(staffCode) || 
				StringUtils.isBlank(deviceType) || StringUtils.isBlank(validCode)){
    		ret.setRetFlag(ReturnMsg.FAIL);
			ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E888);
			ret.setErrorMessage(RegAndBindErrCodeConstants.COMMON_ERR_MAP
	    			.get(RegAndBindErrCodeConstants.COMMON_ERR_E888));
			return gson.toJson(ret);
		}
        try {
        	ret = registerService.createOnlineAndOfflineAccount(staffCode, 
        			deviceType, mobile, validCode, source);
        } catch (Exception e){
        	ret.setRetFlag(ReturnMsg.FAIL);
	    	ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E999);
	    	ret.setErrorMessage(RegAndBindErrCodeConstants.COMMON_ERR_MAP
	    			.get(RegAndBindErrCodeConstants.COMMON_ERR_E999));
	    	Object[] errArgs = {mobile, staffCode, deviceType, validCode, e};
	    	LOGGER.error("注册线上线下会员发生异常:mobile:{}, staffCode:{}, " +
	    			"deviceType:{}, validCode:{}, error:{}", errArgs);
        }
    	return gson.toJson(ret);
    }
}
